<?php namespace Common\model;
use Hdphp\Model\Model;
class shopGoods extends Model{
	//指定表名
	protected $table = "shop_goods";
	//自动验证
	protected $validate = array(
		//array(字段名,验证方法,错误信息,验证条件,验证时间)
		//商品名称
		array('gname','required','商品名称不能为空',3,3),
		//所属分类
		array('cid','required','所属分类不能为空',3,3),
		//所属品牌
		array('bid','required','所属品牌不能为空',3,3),
		//市场价
		array('marketprice','required','市场价不能为空',3,3),
		//商城价
		array('ShopPrice','required','商城价不能为空',3,3),
		//列表图
		array('pic','required','列表图必须上传',3,1),
	);
	
	//自动完成
	protected $auto = array(
		//array(表单字段名,处理方法,方法类型,验证条件,处理时间)
		//时间戳
		array('time','time','function',3,1),
		//保存是用户的uid(也就是知道是哪个用户上传的)
	 	array('uid','userUid','method',3,1),
	 	//货品单号
	 	array('gnumber','gnumber','method',3,1)
	);
	
	//处理字段user_uid的方法，该方法返回什么，数据库就可以存入什么
	public function userUid(){
		return $_SESSION['uid'];
	}

	//处理货品单号
	public function gnumber(){
		$seed  = time() . 'hpdh'; 
		$str = '';
		for ($i=0; $i < 12; $i++) {
			$text = $seed[mt_rand(0, strlen($seed) - 1)];
			$str .= $text;
		}
		return $str;
	}
	
	//自定义的store添加方法
	public function store(){
		if(!$this->create()) return false;
		//1.商品表添加，返回自增id
		$id = $this->add();
		//2.实例化商品详细表
		$goodsDetailModel = new \Common\model\shopGoodsDetail;
		if(!$goodsDetailModel->create()){
			$this->error = $goodsDetailModel->getError();
			return false;
		}
		//把商品id压入商品详细表
		$id = $goodsDetailModel->data['detail_gid'] = $id;
		//执行商品详细表添加
		$goodsDetailModel->add();
		
		//3.实例化商品属性表
		$goodsAttrModel = new \Common\model\shopGoodsAttr;
		//处理属性
		foreach (Q('post.attr',array()) as $k => $v) {
			if($v){
				$attrData = array(
					'gavalue' => $v,
					'goods_gid' => $id,
					'type_taid' => $k
				);
				$goodsAttrModel->add($attrData);
			}
		}
		//处理规格
		foreach (Q('post.spec',array()) as $k => $v) {
			foreach ($v['value'] as $key => $value) {
				$specData = array(
					'gavalue' => $value,
					'extraprice' => $v['extraprice'][$key],
					'goods_gid' => $id,
					'type_taid' => $k
				);
				$goodsAttrModel->add($specData);
			}

		}
		//执行添加
		return true;
	}
	
	
	
	
	//自定义的edit编辑方法
	public function edit(){
		//1.商品表添加
		if(!$this->create()) return false;
		$this->save();
		//2.商品详细表添加
		$goodsDetailModel = new \Common\model\shopGoodsDetail;
		if(!$goodsDetailModel->create()){
			$this->error = $goodsDetailModel->getError();
			return false;
		}
		$goodsDetailModel->save();
		//3.商品属性表添加
		$goodsAttrModel = new \Common\model\shopGoodsAttr;
		//先删除//后添加
		$gid = Q('post.gid',0,'intval');
		$goodsAttrData = $goodsAttrModel->where("goods_gid={$gid}")->delete();
		//处理属性
		foreach (Q('post.attr',array()) as $k => $v) {
			if($v){
				$attrData = array(
					'gavalue' => $v,
					'goods_gid' => $gid,
					'type_taid' => $k
				);
				$goodsAttrModel->add($attrData);
			}
		}
		//处理规格
		foreach (Q('post.spec',array()) as $k => $v) {
			foreach ($v['value'] as $key => $value) {
				$specData = array(
					'gavalue' => $value,
					'extraprice' => $v['extraprice'][$key],
					'goods_gid' => $gid,
					'type_taid' => $k
				);
				$goodsAttrModel->add($specData);
			}

		}
		return true;
	}
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}